#!/bin/bash
# BashChat by BiteTheByte [bitethebyte (at) gmail (dot) com]
# Licensed under GPLv3
# This program is written in bash scripting and comes with absolutely NO WARRANTY
# Be carefull and use it with caution and wisdom.
# :) Enjoy ssh chatting!!!

#$1 directory, $2 prefix_room y $3 escape (when there is no macro...)

#Check for enaugh parameters
if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ] || [ -z "$4" ];
then
	echo "chat_watch Error: Bad parameters"
else
#Check for existing chatroom's history.
if [ -e "$1"/"$2" ];
then
inputmsg="Type your message ($3 to exit)"
#Infinite loop (Everybody knows that Blinker folk will never be "1")
        BlinkerFolk='0'
        while [ "$BlinkerFolk" != "1" ]; do
		#Get the history.
                text=$(cat "$1/$2")
		clear
		#Refreshes the screen and date and asks for new message.
		echo "$text"
		echo ""
		echo -e "\t\t(logged as '$4')"
		echo "$inputmsg"
		echo -e "\t\t\t [$(date +%H:%M)]"
                sleep 3
	#If the history is no longer available, don't refresh.
		if [ ! -e "$1/$2" ]; then
			#I Can't believe it. It's equal to "1" :P
			BlinkerFolk="1"
		echo ""
		#Message only viewable to the user.
		echo "****The room is now closed****"
		echo ""
		fi
        done

else
	echo "chat_watch Error: Chatroom not exists"
fi
fi
